#version 450

layout (binding = 1) uniform samplerCube samplerCubeMap;

layout (location = 0) in vec3 inUVW;

layout (location = 0) out vec4 outPosition;
layout (location = 1) out vec4 outNormal;
layout (location = 2) out vec4 outAlbedo;

const float A = 0.15;
const float B = 0.50;
const float C = 0.10;
const float D = 0.20;
const float E = 0.02;
const float F = 0.30;
const float W = 11.2;

// From http://filmicworlds.com/blog/filmic-tonemapping-operators/
vec3 Uncharted2Tonemap(vec3 color)
{
	return ((color*(A*color+C*B)+D*E)/(color*(A*color+B)+D*F))-E/F;
}

void main()
{
	outPosition.xyz = inUVW * 100.0;
	outPosition.w = -1.0;

	// Tone mapping
	vec3 color = Uncharted2Tonemap(texture(samplerCubeMap, inUVW).rgb * 2.5);
	color = color * (1.0f / Uncharted2Tonemap(vec3(W)));
	// Gamma correction
	color = pow(color, vec3(1.0f / 2.2));
	outAlbedo = vec4(color, 1.0);
}